package com.sunwayworld.basemodule.rm.train.train.service.impl;

import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.module.sys.bpmn.bean.CoreBpmnCommentBean;
import com.sunwayworld.module.sys.bpmn.service.CoreBpmnCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.train.train.dao.LimsTrainRecordDao;
import com.sunwayworld.basemodule.rm.train.train.bean.LimsTrainRecordBean;
import com.sunwayworld.basemodule.rm.train.train.service.LimsTrainRecordService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

@Repository
@GikamBean
public class LimsTrainRecordServiceImpl implements LimsTrainRecordService {

    @Autowired
    private LimsTrainRecordDao limsTrainRecordDao;

    @Autowired
    @Lazy
    private CoreBpmnCommentService coreBpmnCommentService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsTrainRecordDao getDao() {
        return limsTrainRecordDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsTrainRecordBean limsTrainRecord = jsonWrapper.parseUnique(LimsTrainRecordBean.class);
        limsTrainRecord.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsTrainRecord);

        CoreBpmnCommentBean commentBean = new CoreBpmnCommentBean();
        commentBean.setId(ApplicationContextHelper.getNextIdentity());
        commentBean.setTargetId(this.getDao().getTable().concat("$").concat(limsTrainRecord.getId().toString()));
        commentBean.setTaskName("新建");
        commentBean.setRoute("新建");
        commentBean.setUserId(LocalContextHelper.getLoginUserId());
        commentBean.setUserName(LocalContextHelper.getLoginUserName());
        coreBpmnCommentService.getDao().insert(commentBean);

        return limsTrainRecord.getId();
    }

}
